<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="./OL_SDK/include-openlayers-local.js"></script>
  <script src="./libs/gaode.js"></script>
</head>

<body>
  <div id="map">
  </div>
  <script>
    var map = new ol.Map({
      target: "map",
      layers: [gaode],
      view: new ol.View({
        projection: 'EPSG:4326',
        center: [114.30, 30.50],
        zoom: 8
      })
    })
    let url = 'https://geo.datav.aliyun.com/areas_v3/bound/420100_full.json'
    let source = new ol.source.Vector({
      format: new ol.format.GeoJSON(),
      url
    })
    let layer = new ol.layer.Vector({
      source
    })
    map.addLayer(layer)

    map.on('pointermove', function (e) {
      var pixel = map.getEventPixel(e.originalEvent);
      var hit = map.hasFeatureAtPixel(pixel);
      map.getTargetElement().style.cursor = hit ? 'pointer' : '';
    });

    /* 3-2、点击要素popup弹窗出现 */
    const style = new ol.style.Style({
      fill: new ol.style.Fill({
        color: "#333"
      }),
      stroke: new ol.style.Stroke({
        color: "#ff2d51",
        width: 1
      })
    })
    map.on('click', function (evt) {
      //判断当前单击处是否有要素，捕获到要素时弹出popup
      var feature = map.forEachFeatureAtPixel(evt.pixel, function (feature, layer) { return feature; });
      if (feature) {
        // console.log(feature)
        source.forEachFeature(item => {
          item.setStyle(null)
        })
        feature.setStyle(style)
      }
    });
  </script>
</body>

</html>